package net.jcip.chapter4;


import jdk.nashorn.internal.ir.annotations.Immutable;
import net.jcip.annotations.*;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 在 DelegatingVehicleTrcker 中使用的不可变Point类 Immutable Point class used by DelegatingVehicleTracker
 *
 * @author Brian Goetz and Tim Peierls
 */
@Immutable
public class Point {


    public final int x, y;


    private  int z ;

    private Point(int x, int y) {
        this.x = x;
        this.y = y;
    }


    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }


    public int getZ() {
        return z;
    }

    private void setZ(int z) {
        this.z = z;
    }

    public void setX(int x) {
        // this.x = x ;
        System.out.println("ERROR 不能设置 x 属性, x 是 final 权限.");
    }


    public void setY(int y) {
        // this.y= y ;
        System.out.println("ERROR 不能设置 y 属性, y 是 final 权限.");
    }


    @Override
    public String toString() {
        return "Point{" +
                " x=" + x +
                ", y=" + y +
                ", z=" + z +
                '}';
    }

    private static final Integer THREAD_NUM = 100;

    public static void main(String[] args) throws InterruptedException {


        ExecutorService executorService = Executors.newFixedThreadPool(THREAD_NUM);


        CountDownLatch countDownLatch = new CountDownLatch(THREAD_NUM);


        Point point = new Point(0, 0);

        for (int i = 0; i < 1000; i++) {
            int j = i;

            executorService.execute(() -> {

                point.setZ(j);
                countDownLatch.countDown();
            });
        }

        countDownLatch.await();

        executorService.shutdown();

        System.out.println(point.toString());


    }


}
